{extend name="../../../view/public/base" /}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
        <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">

            <!--搜索区域-->
            <div class="layui-card">
                <div class="layui-collapse" lay-accordion="" lay-filter="search">
                    <div class="layui-colla-item">
                        <h2 class="layui-colla-title">查询条件</h2>
                        <div class="layui-colla-content">
                            <form id="searchForm" class="layui-form" lay-filter="searchForm" autocomplete="off">

                                <div class="layui-form-item layui-inline">
                                    <label class="layui-form-label">ID</label>
                                    <div class="layui-input-inline">
                                        <input type="number" name="id" placeholder="请输入ID"
                                               class="layui-input">
                                    </div>
                                </div>

                                <!--应用id 在页面渲染时传过来-->
                                <div class="layui-form-item layui-inline layui-hide">
                                    <label class="layui-form-label">应用ID</label>
                                    <div class="layui-input-inline">
                                        <input type="number" name="miniapp_id" placeholder="请输入应用ID"
                                               class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button class="layui-btn layui-btn-normal" type="button" lay-submit lay-filter="searchBtn">
                                            搜索
                                        </button>
                                        <button class="layui-btn layui-btn-primary" type="button" onclick="resetForm()">
                                            重置
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <!--列表区域-->
            <div class="layui-card" style="background-color: #F2F2F2;">
                <table id="list" lay-filter="listFilter"></table>

                <!--工具栏-->
                <script type="text/html" id="listToolbar">
                    <a class="layui-btn layui-btn-sm layui-bg-black" lay-event="refresh" title="刷新"><i class="layui-icon layui-icon-refresh" style="font-size: 8px !important;"></i> </a>
                    <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" title="新增">新增</a>
                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del" title="删除">删除</a>
                </script>

                <!--后面对应 应该微信绑定对应的user表id-->
                <script type="text/html" id="uid">
                    {{ d.uid }}
                </script>
                <!--客户端ID-->
                <script type="text/html" id="service_id">
                    {{ d.service_id }}
                </script>
                <!--订单ID-->
                <script type="text/html" id="miniapp_order_id">
                    {{ d.miniapp_order_id }}
                </script>
                <!--创建人/购买人-->
                <script type="text/html" id="member_id">
                    {{ d.member_id }}
                </script>
                <!--应用id-->
                <script type="text/html" id="miniapp_id">
                    {{ d.miniapp_id }}
                </script>
                <!--应用名称-->
                <script type="text/html" id="appname">
                    {{ d.appname }}
                </script>
                <!--Logo-->
                <script type="text/html" id="head_img">
                    {{ d.head_img }}
                </script>
                <!--演示二维码-->
                <script type="text/html" id="qrcode_url">
                    {{ d.qrcode_url }}
                </script>
                <!--小程序appid-->
                <script type="text/html" id="miniapp_appid">
                    {{ d.miniapp_appid }}
                </script>
                <!--小程序secret-->
                <script type="text/html" id="miniapp_secret">
                    {{ d.miniapp_secret }}
                </script>
                <!--小程序logo-->
                <script type="text/html" id="miniapp_head_img">
                    {{ d.miniapp_head_img }}
                </script>
                <!--小程序演示二维码-->
                <script type="text/html" id="miniapp_qrcode_url">
                    {{ d.miniapp_qrcode_url }}
                </script>
                <!--导航栏颜色-->
                <script type="text/html" id="navbar_color">
                    {{ d.navbar_color }}
                </script>
                <!--导航栏样式-->
                <script type="text/html" id="navbar_style">
                    {{ d.navbar_style }}
                </script>
                <!--服务时间-->
                <script type="text/html" id="service_time">
                    {{ formatDate(d.service_time) }}
                </script>
                <!--公众号appid-->
                <script type="text/html" id="mp_appid">
                    {{ d.mp_appid }}
                </script>
                <!--公众号secret-->
                <script type="text/html" id="mp_secret">
                    {{ d.mp_secret }}
                </script>
                <!--公众号logo-->
                <script type="text/html" id="mp_head_img">
                    {{ d.mp_head_img }}
                </script>
                <!--公众号演示二维码-->
                <script type="text/html" id="mp_qrcode_url">
                    {{ d.mp_qrcode_url }}
                </script>
                <!--公众号token-->
                <script type="text/html" id="mp_token">
                    {{ d.mp_token }}
                </script>
                <!--公众号AesKey-->
                <script type="text/html" id="mp_aes_key">
                    {{ d.mp_aes_key }}
                </script>
                <!--微信服务商:0=独立应用,1=微信服务商-->
                <script type="text/html" id="is_psp">
                    {{# if (d.is_psp == '0') { }}
                    <span style="color: #1E9FFF;">独立应用</span>
                    {{# } else if(d.is_psp == '1') { }}
                    <span style="color: #1E9FFF;">微信服务商</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_psp}}</span>
                    {{# } }}
                </script>
                <!--服务商appid-->
                <script type="text/html" id="psp_appid">
                    {{ d.psp_appid }}
                </script>
                <!--接入方式:0=独立应用,1=开放平台-->
                <script type="text/html" id="is_open">
                    {{# if (d.is_open == '0') { }}
                    <span style="color: #1E9FFF;">独立应用</span>
                    {{# } else if(d.is_open == '1') { }}
                    <span style="color: #1E9FFF;">开放平台</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_open}}</span>
                    {{# } }}
                </script>
                <!--状态:0=正常,1=锁定-->
                <script type="text/html" id="is_lock">
                    <input type="checkbox" {{ d.is_lock == '0' ? 'checked' : '' }} name="is_lock" value="{{d.id}}" lay-skin="switch" lay-filter="is_lockChange" lay-text="正常|锁定">
                </script>
                <!--create_time-->
                <script type="text/html" id="create_time">
                    {{ formatDate(d.create_time) }}
                </script>
                <!--expire_time-->
                <script type="text/html" id="expire_time">
                    {{ formatDate(d.expire_time) }}
                </script>
                <!--update_time-->
                <script type="text/html" id="update_time">
                    {{ formatDate(d.update_time) }}
                </script>
                <!--mobile-->
                <script type="text/html" id="Member_mobile">
                    {{ d.Member.mobile }}
                </script>
                <!--应用类型-->
                <script type="text/html" id="Miniapp_types">
                    {{# if (d.Miniapp.types.search('mini_program') != -1) { }}
                    <span class='layui-badge'>微信小程序</span>
                    {{# } }}
                    {{# if (d.Miniapp.types.search('app') != -1) { }}
                    <span class='layui-badge'>APP</span>
                    {{# } }}
                    {{# if (d.Miniapp.types.search('h5') != -1) { }}
                    <span class='layui-badge'>h5</span>
                    {{# } }}
                    {{# if (d.Miniapp.types.search('official') != -1) { }}
                    <span class='layui-badge'>微信公众号</span>
                    {{# } }}
                    {{# if (d.Miniapp.types.search('system') != -1) { }}
                    <span class='layui-badge'>系统</span>
                    {{# } }}
                    {{# if (d.Miniapp.types.search('tt_program') != -1) { }}
                    <span class='layui-badge'>头条小程序</span>
                    {{# } }}
                </script>


                <!--状态开关-->
                <script type="text/html" id="listBar">
                </script>
            </div>
        </div>
    </div>
    </div>
</div>

{/block}

{block name="js"}
<script>
    var element, layer, form, table, bwajax, $;

    layui.use(['element', 'layer', 'form', 'table', 'bwajax'], function () {
        element = layui.element;
        layer = layui.layer;
        form = layui.form;
        table = layui.table;
        bwajax = layui.bwajax.instance();
        $ = layui.jquery;

        //用模板渲染的数据渲染form
        bwRenderForm({:json_encode($where)});

        //执行列表
        table.render({
            elem: '#list',
            url: "{:Url('/manage/admin/memberMiniapp/index')}",
            where: getWhere(),
            response: {
                statusCode: 200,  //规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.list //解析数据列表
                };
            },
            // toolbar: '#listToolbar',
            toolbar: false,
            cols: [[
                // {type:'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', align: 'center', width: 80, fixed: 'left'},
                {field: 'Member_mobile', title: '手机号', align: 'center', width: 120, templet: '#Member_mobile'},
                {field: 'appname', title: '应用名称', align: 'center', minWidth: 100, templet: '#appname'},
                {field: 'Miniapp_types', title: '应用类型', align: 'center', minWidth: 100, templet: '#Miniapp_types'},
                {field: 'create_time', title: '创建时间', align: 'center', width: 160, templet: '#create_time'},
                {field: 'expire_time', title: '过期时间', align: 'center', width: 160, templet: '#expire_time'},
                {field: 'service_id', title: '客户端ID', align: 'left', width: 280, templet: '#service_id'},
                {field: 'is_lock', title: '状态', fixed: "right", width: 100, templet: '#is_lock'},
                {field: 'uid', title: '后面对应 应该微信绑定对应的user表id', align: 'center', width: 80, templet: '#uid', hide:true},
                {field: 'miniapp_order_id', title: '订单ID', align: 'center', width: 80, templet: '#miniapp_order_id', hide:true},
                {field: 'member_id', title: '创建人/购买人', align: 'center', width: 80, templet: '#member_id', hide:true},
                {field: 'miniapp_id', title: '应用id', align: 'center', width: 80, templet: '#miniapp_id', hide:true},
                {field: 'head_img', title: 'Logo', align: 'center', minWidth: 150, templet: '#head_img', hide:true},
                {field: 'qrcode_url', title: '演示二维码', align: 'center', minWidth: 150, templet: '#qrcode_url', hide:true},
                {field: 'miniapp_appid', title: '小程序appid', align: 'center', minWidth: 150, templet: '#miniapp_appid', hide:true},
                {field: 'miniapp_secret', title: '小程序secret', align: 'center', minWidth: 150, templet: '#miniapp_secret', hide:true},
                {field: 'miniapp_head_img', title: '小程序logo', align: 'center', minWidth: 150, templet: '#miniapp_head_img', hide:true},
                {field: 'miniapp_qrcode_url', title: '小程序演示二维码', align: 'center', minWidth: 150, templet: '#miniapp_qrcode_url', hide:true},
                {field: 'navbar_color', title: '导航栏颜色', align: 'center', minWidth: 150, templet: '#navbar_color', hide:true},
                {field: 'navbar_style', title: '导航栏样式', align: 'center', minWidth: 150, templet: '#navbar_style', hide:true},
                {field: 'service_time', title: '服务时间', align: 'center', width: 160, templet: '#service_time', hide:true},
                {field: 'mp_appid', title: '公众号appid', align: 'center', minWidth: 150, templet: '#mp_appid', hide:true},
                {field: 'mp_secret', title: '公众号secret', align: 'center', minWidth: 150, templet: '#mp_secret', hide:true},
                {field: 'mp_head_img', title: '公众号logo', align: 'center', minWidth: 150, templet: '#mp_head_img', hide:true},
                {field: 'mp_qrcode_url', title: '公众号演示二维码', align: 'center', minWidth: 150, templet: '#mp_qrcode_url', hide:true},
                {field: 'mp_token', title: '公众号token', align: 'center', minWidth: 150, templet: '#mp_token', hide:true},
                {field: 'mp_aes_key', title: '公众号AesKey', align: 'center', minWidth: 150, templet: '#mp_aes_key', hide:true},
                {field: 'is_psp', title: '微信服务商', align: 'center', width: 100, templet: '#is_psp', hide:true},
                {field: 'psp_appid', title: '服务商appid', align: 'center', minWidth: 150, templet: '#psp_appid', hide:true},
                {field: 'update_time', title: '更新时间', align: 'center', width: 160, templet: '#update_time', hide:true},

                // {title: '操作', toolbar: '#listBar', align: 'center', fixed: "right", width: 140}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        });

        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('/manage/admin/memberMiniapp/add')}",
                    });
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;

                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');

                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);

                    break;
            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('/manage/admin/memberMiniapp/edit')}?id=" + data.id,
                    });
                    break;
                case 'del':
                    del(obj.data.id);
                    break;

            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);

            return false;
        })

        //监听switch开关
        form.on('switch(is_lockChange)', function(obj){
            var state = this.checked ? '0' : '1';

            var id = obj.value;
            bwajax.post("{:Url('/manage/admin/memberMiniapp/edit')}?id="+id, {id:id,is_lock:state})
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('修改成功', {icon: 1});
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                        reload();
                    }
                })
        });
    });
</script>
{/block}